home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / df / df.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-19  |  5.0 KB  |  204 lines

  1. /* 
  2.  * prefix.c --
  3.  *
  4.  *    Program to manipulate the prefix table.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/cmds/df/RCS/df.c,v 1.5 91/08/19 13:03:36 mendel Exp $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20. #include <errno.h>
  21. #include <fs.h>
  22. #include <fsCmd.h>
  23. #include <host.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <status.h>
  27. #include <sysStats.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30.  
  31. /*
  32.  *----------------------------------------------------------------------
  33.  *
  34.  * main --
  35.  *
  36.  *    Main program for "df":  print disk free space info.
  37.  *
  38.  * Results:
  39.  *    None.
  40.  *
  41.  * Side effects:
  42.  *    None.
  43.  *
  44.  *----------------------------------------------------------------------
  45.  */
  46.  
  47. main(argc, argv)
  48.     int argc;
  49.     char *argv[];
  50. {
  51.     register ReturnStatus status = SUCCESS;    /* status of system calls */
  52. #define MAX_PREFIXES 100
  53.     Fs_Prefix prefixes[MAX_PREFIXES];
  54.     struct stat buf;
  55.     int i, numDomains, maxLength;
  56.  
  57.     /*
  58.      * For each argument, stat the argument in order to make sure that
  59.      * there's a prefix table entry loaded for it.  If the file can't
  60.      * be found, then mark the entry so we don't try to print it later.
  61.      */
  62.  
  63.     for (i = 1; i < argc; i++) {
  64.     if (stat(argv[i], &buf) < 0) {
  65.         fprintf(stderr, "%s couldn't find \"%s\": %s.\n", argv[0],
  66.             argv[i], strerror(errno));
  67.     }
  68.     }
  69.  
  70.     /*
  71.      * Collect information for all known domains.
  72.      */
  73.  
  74.     maxLength = 0;
  75.     for (numDomains = 0; ; numDomains++) {
  76.     bzero((char *) &prefixes[numDomains], sizeof(Fs_Prefix));
  77.     status = Sys_Stats(SYS_FS_PREFIX_STATS, numDomains,
  78.         (Address) &prefixes[numDomains]);
  79.     if (status != SUCCESS) {
  80.         break;
  81.     }
  82.     i = strlen(prefixes[numDomains].prefix);
  83.     if (i > maxLength) {
  84.         maxLength = i;
  85.     }
  86.     }
  87.  
  88.     /*
  89.      * If no args were given, then print all domains.  Otherwise just
  90.      * find the ones matching the names given.
  91.      */
  92.  
  93.     if (argc == 1) {
  94.     for (i = numDomains-1; i >= 0; i--) {
  95.         PrintDiskInfo(&prefixes[i], maxLength);
  96.     }
  97.     } else {
  98.     for (i = 1; i < argc; i++) {
  99.         int j;
  100.  
  101.         /*
  102.          * For each of the names given, find the domain that
  103.          * contains the file, by comparing server and domain ids
  104.          * between the file and the prefix table entries.
  105.          */
  106.  
  107.         if (stat(argv[i], &buf) < 0) {
  108.         continue;
  109.         }
  110.         for (j = numDomains-1; j >= 0; j--) {
  111.         if ((prefixes[j].serverID == buf.st_serverID)
  112.             && (prefixes[j].domain == ((int) buf.st_dev))) {
  113.             PrintDiskInfo(&prefixes[j], maxLength);
  114.             break;
  115.         }
  116.         }
  117.     }
  118.     }
  119.     exit(0);
  120. }
  121.  
  122. /*
  123.  *----------------------------------------------------------------------
  124.  *
  125.  * PrintDiskInfo --
  126.  *
  127.  *    Given an Fs_Prefix entry, print disk utilization info for
  128.  *    the prefix.
  129.  *
  130.  * Results:
  131.  *    None.
  132.  *
  133.  * Side effects:
  134.  *    Stuff gets printed on stdout.
  135.  *
  136.  *----------------------------------------------------------------------
  137.  */
  138.  
  139. PrintDiskInfo(prefixPtr, nameSpace)
  140.     register Fs_Prefix *prefixPtr;    /* Information about prefix. */
  141.     int nameSpace;            /* Leave at least this much space
  142.                      * in the "prefix name" column. */
  143. {
  144.     static char serverName[32];
  145.     static int  prevServerID = -1;
  146.     static int  firstTime = 1;
  147.     int        inUse;
  148.     int        free;
  149.     int        avail;
  150.     Host_Entry  *host;
  151.  
  152.     if (firstTime) {
  153.     printf("%-*s  %-10s %10s %10s %10s %9s\n", nameSpace,
  154.         "Prefix", "Server", "KBytes", "Used", "Avail", "% Used");
  155.     firstTime = 0;
  156.     }
  157.     printf("%-*s", nameSpace, prefixPtr->prefix);
  158.  
  159.     if (prefixPtr->serverID > 0) {
  160.  
  161.     /*
  162.      * If the server ID is the same as the previous entry's ID, then
  163.      * we can reuse serverName and save a call to Host_ByID.
  164.      */
  165.     if (prefixPtr->serverID == prevServerID) {
  166.         printf("  %-10s", serverName);
  167.     } else {
  168.         host = Host_ByID(prefixPtr->serverID);
  169.         if (host != (Host_Entry *)NULL) {
  170.         register int charCnt;
  171.         for (charCnt = 0 ; charCnt < sizeof(serverName) ; charCnt++) {
  172.             if (host->name[charCnt] == '.' ||
  173.             host->name[charCnt] == '\0') {
  174.             serverName[charCnt] = '\0';
  175.             break;
  176.             } else {
  177.             serverName[charCnt] = host->name[charCnt];
  178.             }
  179.         }
  180.         serverName[sizeof(serverName)-1] = '\0';
  181.         printf("  %-10s", serverName);
  182.         prevServerID = prefixPtr->serverID;
  183.         } else {
  184.         printf(" (%d)", prefixPtr->serverID);
  185.         }
  186.     }
  187.     } else {
  188.     printf("  %-10s", "(none)");
  189.     }
  190.     if (prefixPtr->domainInfo.maxKbytes <= 0) {
  191.     printf("\n");
  192.     return;
  193.     }
  194.  
  195.     free = prefixPtr->domainInfo.freeKbytes - 
  196.                 (0.1 * prefixPtr->domainInfo.maxKbytes);
  197.     avail = 0.9 * prefixPtr->domainInfo.maxKbytes;
  198.     inUse = avail - free;
  199.  
  200.     printf(" %10d %10d %10d %7d%%\n", 
  201.     prefixPtr->domainInfo.maxKbytes, inUse,    free >= 0 ? free : 0,
  202.     (int) (100.0 * (inUse / (float) avail)));
  203. }
  204.